﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<head>
	<title>ข่าวประชาสัมพันธ์</title>
	<link rel="stylesheet" type="text/css" media="screen" href="css/mystyle.css">
 	<link rel="stylesheet" type="text/css" media="screen" href="css/theme.css">

	<style>
	.paginate{font-family:Arial,Helvetica,sans-serif;padding:3px;margin:3px;}
	.paginate a{padding:2px 5px 2px 5px;margin:2px;border:1px solid #999;text-decoration:none;color:#666;}
	.paginate a:hover,.paginate a:active{border:1px solid #999;color:#000;}
	.paginate span.current{margin:2px;padding:2px 5px 2px 5px;border:1px solid #999;font-weight:bold;background-color:#999;color:#FFF;}
	.paginate span.disabled{padding:2px 5px 2px 5px;margin:2px;border:1px solid #eee;color:#DDD;}
	table.reference
	{
		background-color:#ffffff;
		border:1px solid #c3c3c3;
		border-collapse:collapse;
		width:100%;
	}

	table.reference th
	{
		background-color:#c7eafb; /* #e5eecc; */
		border:1px solid #c3c3c3;
		padding:3px;
		vertical-align:top;
		text-align:left;
	}

	table.reference td 
	{
		border:1px solid #c3c3c3;
		padding:3px;
		vertical-align:top;
	}

	</style>
</head>

<body>
	<div id="wow"></div>
	<div id="layout">
		<?php include('header.php'); ?>

		<div id="main">
			<div id="content">
			  <div class="content-full">
				<h3><img src="pictures/header_news.png" /></h3>
				<div class="home-blog-full">
				
<?php
	
	if(isset($_GET['id']))
	{
		$id = $_GET['id'];
		// open new from id.
		require('database.php');
		$tt = new News();
		$rs = $tt->db_get_news_by_id($id);
		if ($rs == FALSE)
		{
			echo "no result from database";
		}
		else
		{
			echo "<em class='time'>".$tt->db_get_thaidate($rs->date)."</em> ".$rs->topic;
			echo "<br/><br/>";
			echo $rs->body;
			echo "<br/>";
		}
	}
	else
	{
		// list all news with paging
		require('database.php');
		$limit = 15;
		
		if(isset($_GET['page']))
		{
			$page = $_GET['page'];
		}
		else
		{
			$page = 0;
		}

		$tt = new News();
		$total = $tt->db_get_count_allnews();
		if ($page > 0){
			$start = ($page - 1) * $limit;
		}else{
			$start = 0;
		}

		$rs = $tt->db_get_news_paging($limit, $start);

		// Initial page num setup
		$adjacents = 3;
		$targetpage = "news.php";
		if ($page == 0){
			$page = 1;
		}
		$prev = $page - 1;	
		$next = $page + 1;							
		$lastpage = ceil($total/$limit);		
		$LastPagem1 = $lastpage - 1;
		$paginate = '';

		if($lastpage > 1)
		{	
			$paginate .= "<div class='paginate'>";
			// Previous
			if ($page > 1){
				$paginate.= "<a href='$targetpage?page=$prev'>ก่อนหน้า</a>";
			}else{
				$paginate.= "<span class='disabled'>ก่อนหน้า</span>";
			}

			// Pages	
			if ($lastpage < 7 + ($adjacents * 2))	// Not enough pages to breaking it up
			{	
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
					}
				}
			}
			elseif($lastpage > 5 + ($adjacents * 2))	// Enough pages to hide a few?
			{
				// Beginning only hide later pages
				if($page < 1 + ($adjacents * 2))		
				{
					for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
					{
						if ($counter == $page)
						{
							$paginate.= "<span class='current'>$counter</span>";
						}else{
							$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
						}					
					}
					$paginate.= "...";
					$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
					$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
				}
				// Middle hide some front and some back
				elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
				{
					$paginate.= "<a href='$targetpage?page=1'>1</a>";
					$paginate.= "<a href='$targetpage?page=2'>2</a>";
					$paginate.= "...";
					for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
					{
						if ($counter == $page)
						{
							$paginate.= "<span class='current'>$counter</span>";
						}else{
							$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
						}					
					}
					$paginate.= "...";
					$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
					$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
				}
				// End only hide early pages
				else
				{
					$paginate.= "<a href='$targetpage?page=1'>1</a>";
					$paginate.= "<a href='$targetpage?page=2'>2</a>";
					$paginate.= "...";
					for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $page){
							$paginate.= "<span class='current'>$counter</span>";
						}else{
							$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
						}					
					}
				}
			}

			// Next
			if ($page < $counter - 1){ 
				$paginate.= "<a href='$targetpage?page=$next'>ถัดไป</a>";
			}else{
				$paginate.= "<span class='disabled'>ถัดไป</span>";
			}
			$paginate.= "</div>";
		}
	?>
		<table class="reference">
			<tr >
				<th width="120">วันเวลา</th>
				<th width="500">ข่าว</th>
			</tr>
			<?php
			  foreach ($rs as $row){
				echo "<tr>";
				echo "<td><em class='time'>".$tt->db_get_thaidate($row->date)."<em></td>";
				echo "<td>";
				echo "<a href='news.php?id=".$row->id."' >".$row->topic."</a>";
				echo "</td>";
				echo "<tr>";
	  			}
			?>
		</table>
		<br/>
	<?php
		echo $paginate;
	}
	/*
	else
	{
		// list all news with limit = 5 news.
		require('database.php');
		$tt = new News();
		$rs = $tt->db_get_news();
		if ($rs == FALSE)
		{
			echo "no result from database";
		}
		else
		{
			foreach ($rs as $row)
			{
				echo "id =".$row->id;
				echo "<br/>";
				echo "topic =".$row->topic;
				echo "<br/>";
				echo "body =".$row->body;
				echo "<br/>";
				echo "date =".$tt->db_get_thaidate($row->date);
				echo "<br/>";
				echo "<a href='news.php?id=".$row->id."' >link</a>";
				echo "<br/>";
				echo "<br/>";
			}
		}
		echo "count all = ".$tt->db_get_count_allnews();
		echo "</br>";
		$page = $_GET['page'];
		if ($page)
		{
			echo "page = ".$page;
		}
		else
		{
			echo "page = 0";
		}		
	}
	*/
     
?>
					</div>
				</div>
			</div> <!-- /content -->
			<?php include('footer.php'); ?>
		</div> <!-- /main -->
	</div><!-- /layout -->
</body>
</html>

